import {BasePageData} from "../common/core/BasePage";
import {DataLoader, field} from "../../modules/coreModule/data/DataLoader";
import {Task,TaskStatus, TaskType} from "../../modules/taskModule/data/Task";
import {page, pageFunc} from "../common/PageBuilder";
import {ItemDetailPage} from "../common/pages/ItemDetailPage";
import {AuthPage, UserPage} from "../common/partPages/UserPage";
import {PK} from "../../modules/coreModule/data/BaseData";
import {userMgr, waitForLogin} from "../../modules/userModule/managers/UserManager";
import {waitForDataLoad} from "../../modules/coreModule/managers/DataManager";
import {TaskInterface} from "../../modules/taskModule/TaskCommon";
import {MemberInfo, ParticInfo, ParticRecord, ParticRecordStatus} from "../../modules/taskModule/data/ParticRecord";
import {CollectRecord, CollectRecordStatus} from "../../modules/taskModule/data/CollectRecord";
import {alertMgr, handleError, IconType} from "../../modules/coreModule/managers/AlertManager";
import {pageMgr} from "../../modules/coreModule/managers/PageManager";
import {ReportPage} from "../report/ReportPage";
import {TemplateIds} from "../../modules/messageModule/MessageCommon";
import {PaymentPage} from "../payment/PaymentPage";
import {RoleType} from "../../modules/userModule/data/Role";
import {blockLoading, showLoading} from "../../modules/coreModule/managers/LoadingManager";
import {CoreLoading} from "../../modules/coreModule/CoreCommon";
import {UserDetailPage} from "../userDetail/UserDetailPage";

class Data extends BasePageData {

	@field(Task)
	item: Task;

	@field(ParticInfo)
	info: ParticInfo = new ParticInfo();
}

class Param {
	id: PK
}

@page("kidsSignUp", "填写报名信息")
export class KidsSignUpPage extends ItemDetailPage<Data, Task, Param> {

	public data = new Data();

	/**
	 * 部分页
	 */
	public userPage: AuthPage = new AuthPage();

	/**
	 * OnLoad回调
	 */
	onLoad(e) {
		super.onLoad(e);
		this.loadData().then();
	}
	@showLoading(CoreLoading.Mask)
	@blockLoading
	@waitForLogin
	@waitForDataLoad
	private async loadData() {
		if (this.data.info.members.length <= 0)
			this.data.info.members.push(new MemberInfo());

		// const res = await TaskInterface.GetTask({ taskId: this.params.id });
		// const task = DataLoader.load(Task, res.task);
		// await this.setItem(task);
	}

	@pageFunc
	public addChild(){
		let info = this.data.info;
		info.members.push(new MemberInfo());
		this.setData({ info })
	}

	// 获取输入的儿童姓名
	@pageFunc
	public onNameChange(e: CustomEvent<any>) {
		let curIdx = e.currentTarget.dataset.idx;
		let val = e.detail;

		let info = this.data.info;
		info.members[curIdx].name = val;
		this.setData({ info })
	}
	@pageFunc
	public onCardChange(e: CustomEvent<any>) {
		let curIdx = e.currentTarget.dataset.idx;
		let val = e.detail;

		let info = this.data.info;
		info.members[curIdx].cardNo = val;
		this.setData({ info })
	}
	@pageFunc
	public onPhoneChange(e: CustomEvent<any>) {
		let val = e.detail;
		let info = this.data.info;
		info.phone = val;
		this.setData({ info })
	}
	@pageFunc
	public onCNameChange(e: CustomEvent<any>) {
		let val = e.detail;
		let info = this.data.info;
		info.name = val;
		this.setData({ info })
	}

	// 点击弹窗里的删除键，删除已填儿童的信息
	@pageFunc
	public deleteChild(e) {
		let curIdx = e.currentTarget.dataset.idx;
		let info = this.data.info;
		info.members.splice(curIdx, 1);
		this.setData({ info })
	}

	@pageFunc
	public confirm(){
		wx.requestSubscribeMessage({
			tmplIds: [TemplateIds["需求通知"]],
			success: this.doPartic.bind(this)
		})
	}
	private check() {
		if (!this.data.info.name) throw "请填写联系人！";
		if (!this.data.info.phone) throw "请填写联系电话！";
		if (!this.data.info.members.every(m => !!m.name)) throw "报名人员信息不全！";
	}

	@showLoading(CoreLoading.Mask)
	@blockLoading
	@handleError
	private async doPartic() {
		this.check();
		await TaskInterface.ParticTask({
			taskId: this.params.id, partic: true,
			info: this.data.info
		})
		await alertMgr().showToast("报名成功", "success");
		await pageMgr().pop();
	}
}
